// pages/quanzi/list/list.js
const appConfig = require('../../../config/config.js');
const http = require('../../../utils/http.js');
const nav = require('../../../utils/navigator.js');
const tip = require('../../../utils/tip.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navData: [{
      name: "寻宠", //文本
      current: 0, //是否是当前页，0不是  1是
      style: 0, //样式
      ico: 'icon-zhonglixunta', //不同图标
      fn: 'gotoNoticeIndex' //对应处理函数
    }, {
      name: "寻主人",
      current: 0,
      style: 0,
      ico: 'icon-xunren',
      fn: 'gotoPickupIndex'
    }, {
      name: "",
      current: 0,
      style: 1,
      ico: '',
      fn: 'gotopublish'
    }, {
      name: "圈子",
      current: 1,
      style: 0,
      ico: 'icon-xunren',
      fn: 'gotoQuanzi',
    }, {
      name: "我的",
      current: 0,
      style: 0,
      ico: 'icon-wode2',
      fn: 'bindViewMy'
    }],
    imgUrls: [
      'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
      'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
      'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640'
    ], //
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    windowHeight: '',
    windowWidth: '',
    hiddenModal: true,
    pageIndex: {}, //当前页码
    pageData: {}, //分页数据
    loadMoreText: '正在载入更多...', //上拉加载提示文本
    hideBottom: true, //上拉加载提示层,
    postUrl: appConfig.baseUrl + 'Article/QueryArticleList',
    queryType:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    var that = this;
    that.getWindowInfo();
    wx.showLoading({
      title: '玩命加载中...',
    });
    //列表加载
    http.post(appConfig.baseUrl + 'Article/QueryArticleList', {
      PageIndex: 1,
    },
      function (res) { //
        console.log(res.data);

        wx.hideLoading(); //关闭加载动画
        that.setData({
          pageIndex: 1, //当前页码
          pageData: res.data, //分页数据
          hideBottom: true,
          loadMoreText: '正在载入更多...'
        });

    });


  },
  /**
   * 获取屏幕宽高
   */
  getWindowInfo: function() {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        console.log(res);
        // 屏幕宽度、高度
        console.log('height=' + res.windowHeight);
        console.log('width=' + res.windowWidth);
        // 高度,宽度 单位为px
        that.setData({
          windowHeight: res.windowHeight,

          windowWidth: res.windowWidth

        })
      }
    })
  },
  /**
   * 列表点击事件
   */
  item_click:function(e){
    console.log(e.currentTarget.dataset.articleId);
    var articleId = e.currentTarget.dataset.articleId;
    nav.navigateTo("../article/article?articleId=" + articleId);
  },
  /**
   * tab切换
   */
  bindTab_Click:function(e){
    console.log(e.currentTarget.dataset.typeId);
    var that = this;
    var tp = e.currentTarget.dataset.typeId;
    that.setData({
      pageIndex: 1, //当前页码
      postUrl: tp == 0 ? appConfig.baseUrl + 'Article/QueryArticleList' : appConfig.baseUrl + 'Article/QueryAdoptList',
      queryType: tp == 0 ? 0:1
    });

    that.getWindowInfo();
    wx.showLoading({
      title: '玩命加载中...',
    });
    //列表加载
    http.post(that.data.postUrl, {
      PageIndex: 1,
    },
      function (res) { //
        console.log(res.data);

        wx.hideLoading(); //关闭加载动画
        that.setData({
          pageIndex: 1, //当前页码
          pageData: res.data, //分页数据
          hideBottom: true,
          loadMoreText: '正在载入更多...'
        });

      });

  },
  bindPublish:function(e){
    nav.navigateTo("../publish/publish");
  },
  // 下拉刷新
  onPullDownRefresh: function (e) {
    
    var that = this;
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();

    http.post(that.data.postUrl, {
      PageIndex: 1,
    },
      function (res) { //
        console.log(res.data);
        that.setData({
          pageIndex: 1, //当前页码
          pageData: res.data, //分页数据
          hideBottom: true,
          loadMoreText: '正在载入更多...'
        });
        // 隐藏导航栏加载框
        wx.hideNavigationBarLoading();
        // 停止下拉动作
        wx.stopPullDownRefresh();
      });

  },
  //上拉加载
  onReachBottom: function (e) {
    var that = this;
    // 页数+1
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      hideBottom: false
    });

    console.log(that.data.pageIndex);
    http.post(that.data.postUrl, {
      PageIndex: that.data.pageIndex,
    },
      function (res) { //
        console.log(res.data);
        if (res.data.length > 0) {
          var data_list = that.data.pageData;
          for (var i = 0; i < res.data.length; i++) {
            data_list.push(res.data[i]);
          }
          that.setData({
            pageData: data_list, //分页数据
            hideBottom: true
          });
          console.log(that.data.pageData);
        } else {
          //没有更多了
          that.setData({
            loadMoreText: "没有更多数据了", //分页数据
            hideBottom: false,
            pageIndex: that.data.pageIndex - 1
          });
        }
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})